package com.ssh.demo.jschsshdemo.Shell;

/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
/**
 * This program enables you to connect to sshd server and get the shell prompt.
 *   $ CLASSPATH=.:../build javac Shell.java
 *   $ CLASSPATH=.:../build java Shell
 * You will be asked username, hostname and passwd.
 * If everything works fine, you will get the shell prompt. Output may
 * be ugly because of lacks of terminal-emulation, but you can issue commands.
 *
 */

import com.jcraft.jsch.*;
import java.awt.*;
import javax.swing.*;

public class Shell{
    public static void main(String[] arg){

        try{

            //创建jsch
            JSch jsch=new JSch();

            //jsch.setKnownHosts("/home/foo/.ssh/known_hosts");

            String host=null;
//            if(arg.length>0){
//                host=arg[0];
//            }
//            else{
//                host=JOptionPane.showInputDialog("Enter username@hostname",
//                        System.getProperty("user.name")+
//                                "@localhost");
//            }
            //用户名
            String user="root";

            //host.substring(0, host.indexOf('@'));

            //IP地址
            host="10.211.55.3";

            //host.substring(host.indexOf('@')+1);

            //获取session
            Session session=jsch.getSession(user, host, 22);

            String passwd = "root";
            //JOptionPane.showInputDialog("Enter password");
            session.setPassword(passwd);

            UserInfo ui = new MyUserInfo(){
                @Override
                public void showMessage(String message){
                    //JOptionPane.showMessageDialog(null, message);
                }
                @Override
                public boolean promptYesNo(String message){
                    Object[] options={ "yes", "no" };
//                    int foo=JOptionPane.showOptionDialog(null,
//                            message,
//                            "Warning",
//                            JOptionPane.DEFAULT_OPTION,
//                            JOptionPane.WARNING_MESSAGE,
//                            null, options, options[0]);
                    return true;//foo==0;
                }

            };

           session.setUserInfo(ui);


            //session.connect();
            session.connect(30000);   // making a connection with timeout.

            Channel channel=session.openChannel("shell");

            // Enable agent-forwarding.
            //((ChannelShell)channel).setAgentForwarding(true);

            channel.setInputStream(System.in);


            channel.setOutputStream(System.out);


            channel.connect(3*1000);
        }
        catch(Exception e){
            System.out.println(e);
        }
    }

    public static abstract class MyUserInfo
            implements UserInfo, UIKeyboardInteractive{
        @Override
        public String getPassword(){ return null; }
        @Override
        public boolean promptYesNo(String str){ return false; }
        @Override
        public String getPassphrase(){ return null; }
        @Override
        public boolean promptPassphrase(String message){ return false; }
        @Override
        public boolean promptPassword(String message){ return false; }
        @Override
        public void showMessage(String message){ }
        @Override
        public String[] promptKeyboardInteractive(String destination,
                                                  String name,
                                                  String instruction,
                                                  String[] prompt,
                                                  boolean[] echo){
            return null;
        }
    }
}
